package com.dl.helloworld;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CircleView extends View {
    private Paint paint;//画笔对象
    private float lastX, lastY;

    public CircleView(Context context) {
        super(context);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        if (paint == null) {
            paint = new Paint();
        }
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int desiredWidth = 200;  // 默认宽度 200px
        int desiredHeight = 200; // 默认高度 200px
        // 解析 MeasureSpec
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        // 计算实际宽度
        int width;
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize; // 直接使用 XML 中指定的具体值（如 100dp）
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(desiredWidth, widthSize); // wrap_content 时取最小值
        } else {
            width = desiredWidth;
        }
        // 高度同理
        int height;
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(desiredHeight, heightSize);
        } else {
            height = desiredHeight;
        }
        // 设置最终尺寸
        setMeasuredDimension(width, height);
    }

//    @Override
//    protected void onDraw(@NonNull Canvas canvas) {
//        super.onDraw(canvas);
//        //画圆 圆心（x,y）半径radius
//        int radius = Math.min(getWidth(), getHeight()) / 2;
//        canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, paint);
//    }


    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        Path path = new Path();
        path.moveTo(0, 0);          // 起点：左上角
        path.lineTo(getWidth(), 0);  // 右上角
        path.lineTo(getWidth() / 2f, getHeight()); // 底部中点
        path.close(); // 闭合路径

        canvas.drawPath(path, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                lastY = event.getY();
                return true;
            case MotionEvent.ACTION_MOVE:
                float dx = event.getX() - lastX;
                float dy = event.getY() - lastY;
                setX(getX() + dx);
                setY(getY() + dy);
                break;
        }

        return super.onTouchEvent(event);
    }

    void setCircleColor(int color) {
        paint.setColor(color);
        invalidate(); // 必须调用！
    }


}











